package com.xwaf.platform.system.provider.hander;

import com.xwaf.platform.common.utils.http.HttpUtil;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * 
 * <p>
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2020年1月9日 下午12:27:43 <br>
 * @see 无<br>
 *      Copyright (c) 2020, xwaf_love@yeah.net All Rights Reserved.<br>
 */
public class WebSocketNettyChildHandler extends ChannelInitializer<SocketChannel> {

	/**
	 * WebSocketUrl
	 */
	private String webSocketUrl;

	/**
	 * 
	 * 
	 * <p>
	 * Default construction method
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2020年1月9日 下午12:27:32 <br>
	 * @param webSocketUrl
	 */
	public WebSocketNettyChildHandler(String webSocketUrl) {
		this.webSocketUrl = webSocketUrl;
	}

	/**
	 * 初始化Channel
	 */
	@Override
	protected void initChannel(SocketChannel socketChannel) {
		ChannelPipeline pipeline = socketChannel.pipeline();
		// 将请求与应答消息编码或者解码为HTTP消息
		pipeline.addLast("http-codec", new HttpServerCodec());
		// 将http消息的多个部分组合成一条完整的HTTP消息
		pipeline.addLast("aggregator", new HttpObjectAggregator(HttpUtil.MAX_CONTENT_LENGTH));
		// 向客户端发送HTML5文件。主要用于支持浏览器和服务端进行WebSocket通信
		pipeline.addLast("http-chunked", new ChunkedWriteHandler());
		// 服务端Handler
		pipeline.addLast("handler", new WebSocketNettyServerHandler(webSocketUrl));

	}
}
